
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

import netscape.javascript.JSObject;

public class VistaStreetView extends javax.swing.JApplet {

 	private static final long serialVersionUID = 1L;

 	private StreetView streetView;
	private JSObject window;

	public VistaStreetView() {
    	streetView = new StreetView(this);
        initComponents();
    }
    
    public void jsEval(String js) {
    	window.eval(js);
    }
	
    private void initComponents() {
    	try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e1) {
			e1.printStackTrace();
		}
    	
        iniciarBtn = new javax.swing.JButton();
        estadoLbl = new javax.swing.JLabel();
        calibrarBtn = new javax.swing.JButton();
        resetBtn = new javax.swing.JButton();
        detenerBtn = new javax.swing.JButton();
        estadoTimer = new javax.swing.Timer(5000, new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
				estadoLbl.setText(" ");
			}
		});
        
        estadoTimer.setRepeats(false);

        getContentPane().setLayout(new java.awt.FlowLayout());

        iniciarBtn.setToolTipText("Iniciar Servidor");
        iniciarBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("iconos/31.png"))); // NOI18N
        iniciarBtn.setMargin(new java.awt.Insets(2, 5, 2, 5));
        getContentPane().add(iniciarBtn);

        detenerBtn.setToolTipText("Detener Servidor");
        detenerBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("iconos/42.png"))); // NOI18N
        detenerBtn.setMargin(new java.awt.Insets(2, 5, 2, 5));
        detenerBtn.setVisible(false);
        getContentPane().add(detenerBtn);
        
        estadoLbl.setText("MobileFeel SDK Street View Demo"); // NOI18N
        estadoLbl.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEtchedBorder(), BorderFactory.createEmptyBorder(0, 5, 0, 5)));
        estadoLbl.setMaximumSize(new java.awt.Dimension(260, 25));
        estadoLbl.setMinimumSize(new java.awt.Dimension(260, 25));
        estadoLbl.setPreferredSize(new java.awt.Dimension(260, 25));
        getContentPane().add(estadoLbl);

        calibrarBtn.setText("Calibrar"); // NOI18N
        calibrarBtn.setMargin(new java.awt.Insets(2, 8, 2, 8));
        calibrarBtn.setMaximumSize(new java.awt.Dimension(57, 25));
        calibrarBtn.setMinimumSize(new java.awt.Dimension(57, 25));
        calibrarBtn.setPreferredSize(new java.awt.Dimension(57, 25));
        calibrarBtn.setEnabled(false);
        getContentPane().add(calibrarBtn);

        resetBtn.setText("Resetear Calibración"); // NOI18N
        resetBtn.setMargin(new java.awt.Insets(2, 8, 2, 8));
        resetBtn.setMaximumSize(new java.awt.Dimension(119, 25));
        resetBtn.setMinimumSize(new java.awt.Dimension(119, 25));
        resetBtn.setPreferredSize(new java.awt.Dimension(119, 25));
        resetBtn.setEnabled(false);
        getContentPane().add(resetBtn);
        
        iniciarBtn.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
		    	try {
		    		Object puertoStr = JOptionPane.showInputDialog(null, "Ingrese puerto:", "Iniciar Servidor", JOptionPane.PLAIN_MESSAGE, null, null, "51460");
		    		if (puertoStr != null) {
		    			streetView.listen(Integer.parseInt(puertoStr.toString()));
						window = JSObject.getWindow(VistaStreetView.this);
						iniciarBtn.setVisible(false);
						detenerBtn.setVisible(true);
						calibrarBtn.setEnabled(true);
						resetBtn.setEnabled(true);
		    		}
		    	} catch (NumberFormatException ex) {
		    		JOptionPane.showMessageDialog(null, "Puerto no válido.", "Error", JOptionPane.WARNING_MESSAGE);
		    	} catch (Exception ex) {
					JOptionPane.showMessageDialog(null, "Error al abrir socket.", "Error", JOptionPane.WARNING_MESSAGE);
				}
			}});
        
        detenerBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					streetView.stop();
					iniciarBtn.setVisible(true);
					detenerBtn.setVisible(false);
					calibrarBtn.setEnabled(false);
					resetBtn.setEnabled(false);
				} catch (IOException ex) {
					JOptionPane.showMessageDialog(null, "Error al cerrar socket.", "Error", JOptionPane.WARNING_MESSAGE);
				}
			}
		});
        
        calibrarBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				streetView.calibrate();
			}
		});
        
        resetBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				streetView.resetCalibration();
			}
		});
    }// </editor-fold>
    
    public void update(final String s) {
    	java.awt.EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				estadoLbl.setText(s);
				estadoTimer.restart();
			}
		});
    }
    
    /**
     * @param args the command line arguments
     */
     public static void main(String args[]) {
         java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                 new VistaStreetView().setVisible(true);
             }
         });
     }

    // Variables declaration - do not modify
    private javax.swing.JButton calibrarBtn;
    private javax.swing.JLabel estadoLbl;
    private javax.swing.Timer estadoTimer;
    private javax.swing.JButton iniciarBtn;
    private javax.swing.JButton detenerBtn;
    private javax.swing.JButton resetBtn;
    // End of variables declaration

}
